// pages/product/product.js
import { Product } from '../../model/product.js';
import { User } from '../../model/user.js';
var product = new Product();
var user = new User();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    info: {},
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const scene = decodeURIComponent(options.scene)
    if (scene && scene != 'undefined') {
      wx.showLoading({
        title: '加载中',
      });
      this.getProductInfo(scene)
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '专属卡信息为空!',
        showCancel: false,
        confirmText: '返回首页',
        confirmColor: '#F3474E',
        success: function (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/home/home',
            })
          }
        }
      });
    }
  },

  /**
   * 拨打电话
   */
  callPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },

  /**
   * 获取产品详情
   */
  getProductInfo(scene) {
    var WxParse = require('./../../utils/wxParse/wxParse.js');
    product.getProductType(scene, (isVild,result) => {
      if(isVild) {
        WxParse.wxParse('currentContent', 'html', result.desc, this, 5);
        this.setData({
          info: result,
        })
        this.getUserInfo();
      }else{
        wx.hideLoading();
      for (var tmp in result.msg) {
          wx.showModal({
            title: '温馨提示',
            content: result.msg[tmp],
            showCancel: false,
            confirmText: '返回首页',
            confirmColor: '#F3474E',
            success: function (res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }
            }
          });
      }
        
      }
     
    })
  },
  /**
   * 获取用户信息
   */
  getUserInfo() {
    user.getUserInfo((result) => {
      this.setData({
        userInfo: result,
        hidden: true,
      })
      wx.hideLoading();
    })
  },
  /**
   * 订单结算页
   */
  onClickConfirm() {
    const _this = this
    wx.navigateTo({
      url: `/pages/confirm/confirm?id=${_this.data.info.id}&team_user_id=${_this.data.info.team_user_id}`,
    })
  }
})